package com.xishu.controller;

import com.xishu.annotation.valid.MethodValidator;
import com.xishu.entity.customer.VipLevel;
import com.xishu.response.ResponseData;
import com.xishu.response.VerifyUtil;
import com.xishu.service.CommonService;
import com.xishu.service.CompanyService;
import com.xishu.service.CustomerService;
import com.xishu.service.ZhEnService;
import com.xishu.util.Tools;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 会员账号处理
 */
@RestController
public class VipCustomerController implements Tools {
    private static Logger logger = LoggerFactory.getLogger(VipCustomerController.class);
    private CustomerService customerService = CustomerService.getInstance();
    private CompanyService companyService = CompanyService.getInstance();
    private CommonService commonService = CommonService.getInstance();

    @ApiOperation(value = "查询会员等级")
    @PutMapping(value = ("/vip/level"), produces = ("application/json;charset=UTF-8"))
    @MethodValidator(mustLogin = false)
    public ResponseData queryVipLevel(@RequestBody VipLevel vipLevel) throws Exception {
        Long companyId = vipLevel.getCompanyId();
        companyService.findCompanyById(companyId);

        List<VipLevel> vipLevelList = customerService.findVipLevelByCompany(companyId);
        if (!customerService.hasVipLevel(vipLevelList)) {
            customerService.createVipLevel(companyId);
        }

        return commonService.searchResponse(vipLevel, (v) -> {
            if (getBoolean(v.getVip())) {
                v.setName_en("vip");
                v.setName_zh("vip");
                v.setName("vip");
            }
        }, (list) -> {
            for (VipLevel level : list) {
                commonService.dealDefaultValueWithAnnotation(level);
            }

            //这里做下排序
            list = list.stream().sorted(Comparator.comparing(VipLevel::getStartScore)).collect(Collectors.toList());

            return list;
        });
    }

    @ApiOperation(value = "创建会员等级")
    @PostMapping(value = ("/vip/level"), produces = ("application/json;charset=UTF-8"))
    public ResponseData createVipLevel(@RequestBody VipLevel vipLevel) throws Exception {
        boolean own = companyService.ownCompany(vipLevel.getCompanyId());
        VerifyUtil.verify(() -> own);
        vipLevel.setVip(false);
        ZhEnService.getInstance().dealZhEn(vipLevel);
        customerService.checkVipLevelScore(vipLevel);
        return commonService.createObject(vipLevel);
    }

    @ApiOperation(value = "修改会员等级")
    @PutMapping(value = ("/vip/level/modify"), produces = ("application/json;charset=UTF-8"))
    public ResponseData modifyVipLevel(@RequestBody VipLevel vipLevel) throws Exception {
        Optional<VipLevel> vipLevelOptional = commonService.searchOneOptional(vipLevel.getId(), VipLevel.class);
        if (!vipLevelOptional.isPresent()) {
            logger.info("can not find the vip level");
            return ResponseData.emptyResponse();
        }

        boolean own = companyService.ownCompany(vipLevelOptional.get().getCompanyId());
        VerifyUtil.verify(() -> own);
        vipLevel.setVip(null);
        customerService.checkVipLevelScore(vipLevel);
        return commonService.modifyObject(vipLevel);
    }

    @ApiOperation(value = "删除会员等级")
    @DeleteMapping(value = ("/vip/level"), produces = ("application/json;charset=UTF-8"))
    public ResponseData deleteVipLevel(@RequestBody VipLevel vipLevel) throws Exception {
        Optional<VipLevel> vipLevelOptional = commonService.searchOneOptional(vipLevel.getId(), VipLevel.class);
        if (!vipLevelOptional.isPresent()) {
            logger.info("can not find the vip level");
            return ResponseData.emptyResponse();
        }

        boolean own = companyService.ownCompany(vipLevelOptional.get().getCompanyId());
        VerifyUtil.verify(() -> own);

        //VIP的等级不能够删除
        VipLevel vipLevelInDb = vipLevelOptional.get();
        VerifyUtil.verify(() -> !getBoolean(vipLevelInDb.getVip()));

        return commonService.delete(vipLevel);
    }


}
